package view;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import model.Prescription;
import controller.CustomEvent;
import controller.ToolBoxEventHandler;
import view.toolboxComponents.*;
/**
 * Class that builds up the toolbox to provide the functionality to the user
 * @author Daniel Bond, Gideon Pyzer, Mohammed Abdullah, Marvin Crawford
 *
 */
@SuppressWarnings("serial")
public class Toolbox extends JPanel
{
	//Labels with no specific panels
	private JLabel lblToolBox;
	private JLabel lblObjects;
	private JLabel lblBGOptions;
	private JLabel lblObjectOptions;
	//Buttons with no specific panels
	private JButton btnAddObject;
	private JButton btnDelete;
	
	//Add the different panels
	private PrescriptionBackgroundPanel prescriptionBackgroundPanel;
	private PrescriptionHeightPanel prescriptionHeightPanel;
	private PrescriptionLineColourPanel prescriptionLineColourPanel;
	private PrescriptionLineWidthPanel prescriptionLineWidthPanel;
	private PrescriptionNamePanel prescriptionNamePanel;
	private PrescriptionObjectFillPanel prescriptionObjectFillPanel;
	private PrescriptionObjectNamePanel prescriptionObjectNamePanel;
	private PrescriptionSerialNumberPanel prescriptionSerialNumberPanel;
	private PrescriptionTextColourPanel prescriptionTextColourPanel;
	private PrescriptionWidthPanel prescriptionWidthPanel;
	//Event handler for toolbox
	private ToolBoxEventHandler eventHandler;
	private static Toolbox instance;

	private Toolbox()
	{
		//Set up event handler
		eventHandler = new ToolBoxEventHandler();
		//Draw the tool box
		drawToolBox();
	}
	/**
	 * gets the instance of the toolbox 
	 * @return the instance of the toolbox
	 */
	public static Toolbox getInstance(){
		if(instance == null){
			instance = new Toolbox();
		}
		return instance;
	}
	/**
	 * draws the toolbox to the screen
	 */
	private void drawToolBox()
	{
		this.setPreferredSize(new Dimension(200,0));
		//this.setBackground(new Color(60,132,83));
		//Define the panels
		prescriptionBackgroundPanel = new PrescriptionBackgroundPanel();
		prescriptionHeightPanel = new PrescriptionHeightPanel();
		prescriptionLineColourPanel = new PrescriptionLineColourPanel();
		prescriptionLineWidthPanel = new PrescriptionLineWidthPanel();
		prescriptionNamePanel = new PrescriptionNamePanel();
		prescriptionObjectFillPanel = new PrescriptionObjectFillPanel();
		prescriptionObjectNamePanel = new PrescriptionObjectNamePanel();
		prescriptionSerialNumberPanel = new PrescriptionSerialNumberPanel();
		prescriptionTextColourPanel = new PrescriptionTextColourPanel();
		prescriptionWidthPanel = new PrescriptionWidthPanel();
		
		//Icon Images for Buttons
		
		//Labels
		lblToolBox = new JLabel("Tool Box");
		lblObjects = new JLabel("Objects");
		lblBGOptions = new JLabel("Prescription Options");
		lblObjectOptions = new JLabel("Object Options");
		//Buttons
		btnAddObject = new JButton("Add Object");
	
		btnDelete = new JButton("Delete");
		//Properties for objects
		btnAddObject.setMaximumSize(new Dimension(100, 20));
		btnAddObject.setPreferredSize(new Dimension(100, 20));
		btnAddObject.setToolTipText("Add a Box");
		
		btnDelete.setMaximumSize(new Dimension(90, 20));
		//set the border for tool box
		this.setBorder(new EmptyBorder(5,5,5,5));
		//Set the layout for the tool box. BoxLayout
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		//Add Items to Tool Box Panel
		this.add(lblToolBox);
		this.add(btnDelete);
		this.add(Box.createVerticalStrut(20));
		this.add(lblObjects);
		this.add(Box.createVerticalStrut(5));
		this.add(btnAddObject);
		this.add(Box.createVerticalStrut(2));
		this.add(Box.createVerticalStrut(20));
		//Prescription Options
		this.add(lblBGOptions);
		this.add(Box.createVerticalStrut(5));
		this.add(prescriptionBackgroundPanel);
		this.add(Box.createVerticalStrut(5));
		this.add(prescriptionNamePanel);
		this.add(Box.createVerticalStrut(5));
		this.add(prescriptionSerialNumberPanel);
		this.add(Box.createVerticalStrut(5));
		this.add(prescriptionWidthPanel);
		this.add(Box.createVerticalStrut(5));
		this.add(prescriptionHeightPanel);
		this.add(Box.createVerticalStrut(25));
		//Object Options
		this.add(lblObjectOptions);	
		this.add(Box.createVerticalStrut(10));
		this.add(prescriptionObjectNamePanel);
		this.add(Box.createVerticalStrut(5));
		this.add(prescriptionLineColourPanel);
		this.add(Box.createVerticalStrut(10));
		this.add(prescriptionLineWidthPanel);
		this.add(Box.createVerticalStrut(10));
		this.add(prescriptionObjectFillPanel);
		this.add(Box.createVerticalStrut(10));
		this.add(prescriptionTextColourPanel);
		//this.add(Box.createVerticalStrut(20));
		//Add listeners for button in tool box
		//Add a box to the canvas with default settings
		btnAddObject.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				CustomEvent event = new CustomEvent("presAddObject", this);
				Toolbox.this.eventHandler.createEvent(event);
			}
		});
		
		btnDelete.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				CustomEvent event = new CustomEvent("presDeleteObj", this);
				Toolbox.this.eventHandler.createEvent(event);
			}
		});
	}
	/**
	 * Updates fields according to properties from Prescription
	 * @param selected - Selected Object Index from Designer
	 */
	public void update(int selected){
		Prescription prescription = Prescription.getInstance();
		if(prescription.objects().size() > 0){
			prescriptionBackgroundPanel.setButtonBackgroundColour(prescription.getBGColour());
			prescriptionHeightPanel.setText(Integer.toString(prescription.getHeight()));
			prescriptionLineColourPanel.setButtonBackgroundColour(prescription.objects().get(selected).getLineColour());
			prescriptionLineWidthPanel.setSelected(prescription.objects().get(selected).getLineWidth());
			prescriptionNamePanel.setText(prescription.getName());
			prescriptionObjectFillPanel.setButtonBackgroundColour(prescription.objects().get(selected).getFillColour());
			prescriptionObjectNamePanel.setText(prescription.objects().get(selected).getName());
			prescriptionSerialNumberPanel.setText(prescription.getSerialNo());
			prescriptionTextColourPanel.setButtonBackgroundColour(prescription.objects().get(selected).getTextColour());
			prescriptionWidthPanel.setText(Integer.toString(prescription.getWidth()));
		}
	}
	/**
	 * resets the tool box to a blank state.
	 */
	public void resetToolbox()
	{
		for (int i = 0; i < getComponents().length; i++)
		{
			if (this.getComponent(i) instanceof AbstractPanel)
			{
				AbstractPanel panel = (AbstractPanel) this.getComponent(i);
				panel.resetComponent();
			}
		}
		this.repaint();
	}
}